import json
import os
import time
import re
base = './dump'

def dump_text(text: str, file_path: str = None, prefix: str = None, base: str = base):
    if not file_path:
        ts = time.time()
        file_path = f"output_{ts}.txt"
    if prefix:
        file_path = f"{prefix}_{file_path}"
    with open(os.path.join(base, file_path), "w", encoding="utf-8") as f:
        f.write(text)

def dump_json(data: dict, file_path: str = None, prefix: str = None, base: str = base):
    if not file_path:
        ts = time.time()
        file_path = f"output_{ts}.json"
    if prefix:
        file_path = f"{prefix}_{file_path}"
    with open(os.path.join(base, file_path), "w", encoding="utf-8") as f:
        json.dump(data, f, ensure_ascii=False, indent=4)

def load_json(file_path: str):
    with open(os.path.join(base, file_path), "r", encoding="utf-8") as f:
        return json.load(f)

def load_text(file_path: str):
    with open(os.path.join(base, file_path), "r", encoding="utf-8") as f:
        return f.read()

def clean_text(text):
    text = re.sub(r'\s+', ' ', text)
    if text[-1] == '.':
        text = text[:-1]
    # text = re.sub(r'^[\W_]+|[\W_]+$', '', text)
    
    return text